package com.lee.commonerror.code.lock;

import com.lee.commonerror.common.annotation.Wrong;
import lombok.extern.slf4j.Slf4j;

/**
 * 线程不安全导致的
 * @author ：liylw
 * @date ：Created in 2021/4/12 16:48
 */
@Wrong
@Slf4j
public class InterestingWrong {

    volatile int a = 1;
    volatile int b = 1;

    public void add(){
        log.info("add start");
        for (int i = 0; i < 10000; i++) {
            a++;
            b++;
        }
        log.info("add done");
    }

    public void compare(){
        log.info("compare start");
        for (int i = 0; i < 10000; i++) {
            if (a<b){
                log.info("a:{},b:{},{}",a,b,a>b);
            }
        }
        log.info("compare done");
    }

    public static void main(String[] args) {
        InterestingWrong interestingWrong = new InterestingWrong();
        new Thread(()-> interestingWrong.add()).start();
        new Thread(()-> interestingWrong.compare()).start();
    }
}
